package lc.util;

import com.jme.math.Vector2f;
import com.jme.math.Vector3f;

//	Utility to efficiently store and grow an array of floats (not using boxing)
public final class SimpleFloatArray {

	public float[] data;
	public int logicalSize;

	public final void add(Vector3f element) {
		add(element.x, element.y, element.z);
	}

	public final void add(Vector2f element) {
		maybeExpand();
		data[logicalSize++] = element.x;
		data[logicalSize++] = element.y;
	}

	public final void add(float x) {
		maybeExpand();
		data[logicalSize++] = x;
	}

	public final void add(float x, float y, float z) {
		maybeExpand();
		data[logicalSize++] = x;
		data[logicalSize++] = y;
		data[logicalSize++] = z;
	}

	public final void get3f(int i, Vector3f out) {
		out.x = data[i];
		out.y = data[i+1];
		out.z = data[i+2];
	}

	public final void set3f(int i, Vector3f in) {
		data[i] = in.x;
		data[i+1] = in.y;
		data[i+2] = in.z;
	}

	public final float[] construct() {
		if (data == null) return null;
		float[] ret = new float[logicalSize];
		for (int i = 0, n = logicalSize; i != n; ++i) {
			ret[i] = data[i];
		}
		return ret;
	}

	private final void maybeExpand() {
		if (data == null) {
			data = new float[10];
		}
		else if (logicalSize + 3 > data.length) {
			float[] nuData = new float[((int)(data.length * 1.5) + 5)];
			for (int i = 0, n = data.length; i < n; ++i) {
				nuData[i] = data[i];
			}
			data = nuData;
		}
	}
}

